Skip to content

[FEAT] 실시간 채팅 시스템 구현 (WebSocket STOMP + Redis Pub/Sub)#33

Merged
nickhealthy merged 7 commits intodevelopfrom
feat/chat-web-socket
Feb 27, 2026
Merged

[FEAT] 실시간 채팅 시스템 구현 (WebSocket STOMP + Redis Pub/Sub)#33
nickhealthy merged 7 commits intodevelopfrom
feat/chat-web-socket

Conversation

@nickhealthy
Copy link
Copy Markdown
Member

@nickhealthy nickhealthy commented Feb 27, 2026

개요

WebSocket STOMP 프로토콜과 Redis Pub/Sub을 기반으로 실시간 채팅 시스템을 구현합니다.
스케일 아웃 환경에서도 다중 서버 간 메시지 브로드캐스트가 가능한 구조입니다.

구현 범위

인프라 / 설정

  • WebSocketConfig — STOMP 엔드포인트 /ws, 브로커 /topic /queue, 발행 prefix /app
  • StompChannelInterceptor — CONNECT 시 JWT 토큰 검증 후 Principal 설정
  • SecurityConfig/ws/** 경로 인증 없이 허용 (WebSocket handshake는 STOMP 레이어에서 인증)
  • RedisConfig — JSON 직렬화 chatRedisTemplate, RedisMessageListenerContainer 추가

Redis 실시간 서비스

서비스 역할
ChatMessagePublisher chat:room:{roomId} 채널로 메시지 발행
ChatMessageSubscriber 채널 구독 후 SimpMessagingTemplate으로 STOMP 브로드캐스트
PresenceRedisService 온라인 상태 관리 (ZSet, TTL 5분)
SessionRedisService 서버 인스턴스별 WebSocket 세션 추적 (Hash, TTL 30분)
UnreadCountRedisService 원자적 읽지 않은 메시지 수 관리 (INCR/DECR)
MessageCacheRedisService 최근 메시지 50개 캐시 (List)

WebSocket 핸들러 (STOMP)

클라이언트 발행 경로 동작
/app/chat/{roomId}/send 메시지 저장 + Redis Pub/Sub 브로드캐스트
/app/chat/{roomId}/typing 타이핑 이벤트 브로드캐스트
/app/chat/{roomId}/read 읽음 처리 (Redis + DB 동기화)
/app/chat/{roomId}/join 채팅방 입장 (Presence 갱신)
/app/chat/{roomId}/leave 채팅방 퇴장 (Presence 제거)

REST API

메서드 경로 설명
POST /api/chat/rooms 채팅방 생성 (1:1 / 그룹)
GET /api/chat/rooms 내 채팅방 목록 조회
GET /api/chat/rooms/{roomId}/messages/latest 최신 메시지 N개 조회 (커서 초기화)
GET /api/chat/rooms/{roomId}/messages/sync 재접속 누락 복구 (afterMessageId 기준)
GET /api/chat/rooms/{roomId}/messages/before 과거 더보기 (beforeMessageId 기준)

메시지 페이징 전략

createdAt 대신 PK(id) 기반 커서 페이징 적용

  • 동일 시각 메시지 중복/누락 없음
  • afterMessageId — 재접속 시 누락 복구
  • beforeMessageId — 무한 스크롤 과거 조회

커밋 구조

aeab863 feat: WebSocket STOMP 채팅 인프라 설정 (JWT 인터셉터, 보안 설정)
7ab3830 chore: 채팅 기능 공통 인프라 설정 (Redis 템플릿, 스케줄링, 에러코드)
bf52ab5 feat: Redis 기반 실시간 서비스 구현 (Pub/Sub, Presence, 세션, 캐시)
4efd53b feat: 채팅방/참여자 서비스 및 REST API 구현
18cb119 feat: 채팅 메시지 서비스 및 커서 기반 페이징 REST API 구현
105e38f feat: WebSocket STOMP 실시간 메시지 핸들러 구현

테스트

  • ChatMessageRepositoryTest — 커서 기반 조회 3개 케이스
  • ChatMessageServiceTest — 메시지 전송, 읽음 처리, 커서 페이징
  • ChatRoomServiceTest — 1:1 채팅방 생성, 중복 방지
  • ChatParticipantServiceTest — 참여자 관리, 읽지 않은 메시지 DB 동기화

nickhealthy and others added 6 commits February 27, 2026 14:17
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@nickhealthy nickhealthy self-assigned this Feb 27, 2026
@nickhealthy nickhealthy merged commit cb5951e into develop Feb 27, 2026
1 check passed
@nickhealthy nickhealthy deleted the feat/chat-web-socket branch February 27, 2026 06:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant